<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
if (!defined('IN_IPB')) {
    print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded all the relevant files.";
    exit();
}
class diary{

    # Classes

    var $ipsclass;

    # Others
    var $output = "";
    var $page_title = "";
    var $common;
    var $nav = array();
    var $page = "";
    var $html = "";
    
    function auto_run(){
        $this->ipsclass->load_language('lang_regportal');
        $this->ipsclass->load_template('skin_regportal');
        require_once( ROOT_PATH . 'sources/action_public/Common.php' );
        $this->common = new Common();
        $this->common->ipsclass = & $this->ipsclass;
        require ROOT_PATH . "sources/classes/class_email.php";

        $this->email = new emailer();
        $this->email->ipsclass = & $this->ipsclass;
        $this->email->email_init();
        
        if (USE_MODULES == 1) {
            require ROOT_PATH . "modules/ipb_member_sync.php";

            $this->modules = new ipb_member_sync();
            $this->modules->ipsclass = & $this->ipsclass;
        }
        //$this->ipsclass->pageTitle = $this->ipsclass->lang['page_diary_title'];
        //$this->output .= $this->ipsclass->compiled_templates['skin_regportal']->simplepathway();
        //echo $this->ipsclass->input['CODE'];exit();
        switch ($this->ipsclass->input['CODE']) {
            case "news_post":
                break;
            case "diaries":
                $this->get_diaries();
                break;
            case "diaries_list":
                $this->ipsclass->pageTitle = $this->ipsclass->lang['page_diary_title'];
                $this->list_diaries();
                break;
            case "diary_detail_ajax":
                $this->ipsclass->pageTitle = $this->ipsclass->lang['page_diary_title'];
                $this->get_diary_detail_ajax();
                break;
            case "diary_detail":
                $this->get_diary_detail();
                break;
            default :
                $this->get_diaries();
                break;
        }
        $this->ipsclass->getpage=$this->page;
        
        $this->output .=  $this->common->load_right_content();
        $this->ipsclass->print->add_output($this->output);
        $this->ipsclass->print->do_output(array('TITLE' => $this->page_title, 'JS' => 1, 'NAV' => '','PAGE'=>$this->page));
    }
    function get_diary_detail(){
        $this->ipsclass->load_template('skin_profile');
        require_once( ROOT_PATH . "sources/components_public/profile/topics.php" );
        $profile_topics = new profile_topics;
        $profile_topics->ipsclass = & $this->ipsclass;
        require_once(KERNEL_PATH . "class_textcode.php");
        $this->ipsclass->textcode = new class_textcode();
        $member_id = $this->ipsclass->input['profile_id'];
        
        if (intval($member_id) <= 0 )
        {
            $this->ipsclass->boink_it( $this->ipsclass->base_url );
            exit();
        }
        $this->ipsclass->DB->simple_construct(array('select'    => 'id',
                                                    'from'		=> 'members',
                                                    'where'		=> "id={$member_id}"
                                                    )
                                            );
        $this->ipsclass->DB->simple_exec();
        $result = $this->ipsclass->DB->fetch_row();
        $member_id = $result['id'];
        if (intval($member_id) <= 0 )
        {
            $this->ipsclass->boink_it( $this->ipsclass->base_url );
            exit();
        }
        $member = $this->common->format_member($member_id);
        $this->output .= $this->common->load_left_content($member, TRUE, FALSE);
        $this->output .= $this->ipsclass->compiled_templates['skin_profile']->div_content();
        $this->output .= $profile_topics->show_topic_detail($member, array(37));
        $this->output .= $this->ipsclass->compiled_templates['skin_profile']->end_div_content();
        //$this->output .=  $this->common->load_right_content();
    }
    function get_diary_detail_ajax(){
        
        $member_id = $this->ipsclass->member['id'];
        
        if(isset($this->ipsclass->input['profile_id']) && intval($this->ipsclass->input['pro_id']) > 0){
            $member_id = $this->ipsclass->input['profile_id'];
        }else 
        if(isset($this->ipsclass->input['pro_id']) && intval($this->ipsclass->input['pro_id']) > 0){
            $member_id = $this->ipsclass->input['pro_id'];
        }
        
        if (intval($member_id) <= 0 )
        {
            $this->ipsclass->boink_it( $this->ipsclass->base_url );
            exit();
        }
        $this->ipsclass->DB->simple_construct(array('select'    => 'id',
                                                    'from'		=> 'members',
                                                    'where'		=> "id={$member_id}"
                                                    )
                                            );
        $this->ipsclass->DB->simple_exec();
        $result = $this->ipsclass->DB->fetch_row();
        $member_id = $result['id'];
        if (intval($member_id) <= 0 )
        {
            $this->ipsclass->boink_it( $this->ipsclass->base_url );
            exit();
        }
        $member = $this->common->format_member($member_id);
        
        $this->ipsclass->load_template('skin_profile');
        require_once( ROOT_PATH . "sources/components_public/profile/topics.php" );
        $profile_topics = new profile_topics;
        $profile_topics->ipsclass = & $this->ipsclass;
        require_once(KERNEL_PATH . "class_textcode.php");
        $this->ipsclass->textcode = new class_textcode();
        if($this->ipsclass->member['id'] == $member['id']){
            $is_panel = FALSE;
            $is_msg = TRUE;
        }
        $this->output .= $this->common->load_left_content($member, $is_msg, TRUE, $is_panel);
        $this->output .= $this->ipsclass->compiled_templates['skin_profile']->div_content();
        $this->output .= $profile_topics->show_topic_detail($member, array(37));
        $this->output .= $this->ipsclass->compiled_templates['skin_profile']->end_div_content();
        
        
    }
    function list_diaries(){
        $per_page = intval($this->ipsclass->vars['social_per_page_setting']) <=0 ? 25 :  $this->ipsclass->vars['social_per_page_setting'];
        $start = intval($this->ipsclass->input['st']) >= 0 ? intval($this->ipsclass->input['st']) : 0;
        $member_id = $this->ipsclass->input['profile_id'];
        if (intval($member_id) <= 0 )
        {
            $this->ipsclass->boink_it( $this->ipsclass->base_url );
            exit();
        }
        $this->ipsclass->DB->simple_construct(array('select'    => 'id',
                                                    'from'		=> 'members',
                                                    'where'		=> "id={$member_id}"
                                                    )
                                            );
        $this->ipsclass->DB->simple_exec();
        $result = $this->ipsclass->DB->fetch_row();
        $member_id = $result['id'];
        if (intval($member_id) <= 0 )
        {
            $this->ipsclass->boink_it( $this->ipsclass->base_url );
            exit();
        }
        $member = $this->common->format_member($member_id);
        require_once( KERNEL_PATH . 'class_textcode.php');
        $class_textcode = new class_textcode();
        $this->ipsclass->textcode = $class_textcode;
        require_once( ROOT_PATH . "sources/portal_plugins/recent_topics.php" );
        $this->recent_topic = new ppi_recent_topics();
        $this->recent_topic->ipsclass =& $this->ipsclass;
        // Prepare for building url by BabyWolf
        $member['uname'] = strtolower($this->ipsclass->textcode->RemoveAccent($member['name'],"-"));
        //-----------------------------------------
        // Got stuff?
        //-----------------------------------------
        $this->ipsclass->load_template('skin_regportal');
        $this->ipsclass->load_language('lang_regportal');
        $where = "t.starter_id = {$member_id} and t.forum_id = 37 group by concat(t.tid) order by p.pid DESC " ; // and post <> 0
        $this->ipsclass->DB->flush_query();
        $this->ipsclass->DB->build_query( array( 'select'   => 'p.*',
                                                 'from'     => array( 'posts' => 'p' ),
                                                 'where'    => $where,
                                                 'limit'         => array($start, $per_page),
                                                 'add_join' => array(0 => array(
                                                                                 'select'=>'t.*',
                                                                                 'from'   => array( 'topics' => 't' ),
                                                                                 'where'    => 'p.topic_id = t.tid',
                                                                                 'type'   => 'left'
                                                                                ),
                                                                      1 => array( 'select' => 'f.*',
                                                                                      'from'   => array( 'forums' => 'f' ),
                                                                                      'where'  => 'f.id=t.forum_id',
                                                                                      'type'   => 'left' ))
                                                )

            );
        $this->ipsclass->DB->exec_query();	
        $diaries = array();
        while($row = $this->ipsclass->DB->fetch_row()){
            $row['temp_title'] = strtolower($this->replace_unicode($this->filter_title($row['title']))) ;
            $row['temp_title'] = preg_replace('/[^A-Za-z0-9_-]+/', '', $row['temp_title']);
            $row['start_date'] = $this->ipsclass->get_date($row['start_date'],"LONG");
            $row['post'] = $this->recent_topic->Bw_CutString($row['post'], 500);
            $diaries[] = $row;
        }
        $stats = $this->ipsclass->DB->simple_exec_query(array( 'select'   => 'count(p.pid) as count ',
                                                 'from'     => array( 'posts' => 'p' ),
                                                 'where'    => $where,
                                                 
                                                 'add_join' => array(0 => array(
                                                                                 
                                                                                 'from'   => array( 'topics' => 't' ),
                                                                                 'where'    => 'p.topic_id = t.tid',
                                                                                 'type'   => 'left'
                                                                                ),
                                                                      1 => array( 
                                                                                      'from'   => array( 'forums' => 'f' ),
                                                                                      'where'  => 'f.id=t.forum_id',
                                                                                      'type'   => 'left' ))
                                                )
        );
        /*
         * Paging
         */
        $pages = $this->common->create_customize_paging($stats['count'], $per_page, $start, "", $this->ipsclass->lang['tpl_pages'],'#', TRUE, "diaries_paging");
        $vars = array();
        $vars['pages'] = $pages;
        $vars['total_diaries'] = count($diaries);
        $vars['diaries'] = $diaries;
        $vars['profile_id'] = $member_id;
        $vars['profile_name'] = $member['name'];
        if($this->ipsclass->member['id'] == $member['id']){
            $is_msg = TRUE;
        }
        if($member_id == $member['id']){
            $is_panel = TRUE;
        }
        $diary_layout = $this->ipsclass->compiled_templates['skin_regportal']->skin_diaries($vars);
        $this->output .= $this->common->load_left_content($member, $is_msg, TRUE, $is_panel, FALSE);
        $this->output .= $this->ipsclass->compiled_templates['skin_regportal']->skin_profile_diaries( $member, $diary_layout, $friends, $visitors);
    }
    function get_diaries($per_page="", $start=""){
        $member_id = $this->ipsclass->input['po_id'];
        if (intval($member_id) <= 0 )
        {
            $this->ipsclass->boink_it( $this->ipsclass->base_url );
            exit();
        }
        $this->ipsclass->DB->simple_construct(array('select'    => 'id',
                                                    'from'		=> 'members',
                                                    'where'		=> "id={$member_id}"
                                                    )
                                            );
        $this->ipsclass->DB->simple_exec();
        $result = $this->ipsclass->DB->fetch_row();
        $member_id = $result['id'];
        if (intval($member_id) <= 0 )
        {
            $this->ipsclass->boink_it( $this->ipsclass->base_url );
            exit();
        }
        $member = $this->common->format_member($member_id);
        require_once( ROOT_PATH . "sources/components_public/profile/topics.php" );
        $profile_topics = new profile_topics;
        $profile_topics->ipsclass = & $this->ipsclass;
        //return $profile_topics->show_topic_list($this->ipsclass->member, array(37));
        
        require_once( ROOT_PATH . "sources/portal_plugins/recent_topics.php" );
        
        $this->recent_topic = new ppi_recent_topics();
        $this->recent_topic->ipsclass =& $this->ipsclass;
        
        $this->ipsclass->load_template('skin_regportal');
        $this->ipsclass->load_language('lang_regportal');
        $per_page = intval($per_page) <=0 ? 25 :  $per_page;
        $start = intval($start) >= 0 ? intval($start) : 0;
        $where = "t.starter_id = {$member_id} and t.forum_id = 37 group by concat(t.tid) order by p.pid DESC " ; // and post <> 0
        $this->ipsclass->DB->flush_query();
        $this->ipsclass->DB->build_query( array( 'select'   => 'p.*',
                                                 'from'     => array( 'posts' => 'p' ),
                                                 'where'    => $where,
                                                 'limit'         => array($start, $per_page),
                                                 'add_join' => array(0 => array(
                                                                                 'select'=>'t.*',
                                                                                 'from'   => array( 'topics' => 't' ),
                                                                                 'where'    => 'p.topic_id = t.tid',
                                                                                 'type'   => 'left'
                                                                                ),
                                                                      1 => array( 'select' => 'f.*',
                                                                                      'from'   => array( 'forums' => 'f' ),
                                                                                      'where'  => 'f.id=t.forum_id',
                                                                                      'type'   => 'left' ))
                                                )

            );
        $this->ipsclass->DB->exec_query();	
        
        $diaries = array();
        while($row = $this->ipsclass->DB->fetch_row()){
            $row['temp_title'] = strtolower($this->replace_unicode($this->filter_title($row['title']))) ;
            $row['temp_title'] = preg_replace('/[^A-Za-z0-9_-]+/', '', $row['temp_title']);
            $row['start_date'] = $this->ipsclass->get_date($row['start_date'],"LONG");
            $row['post'] = $this->recent_topic->Bw_CutString($row['post'], 500);
            $diaries[] = $row;
        }
        $stats = $this->ipsclass->DB->simple_exec_query(array( 'select'   => 'count(p.pid) as count ',
                                                 'from'     => array( 'posts' => 'p' ),
                                                 'where'    => $where,
                                                 
                                                 'add_join' => array(0 => array(
                                                                                 
                                                                                 'from'   => array( 'topics' => 't' ),
                                                                                 'where'    => 'p.topic_id = t.tid',
                                                                                 'type'   => 'left'
                                                                                ),
                                                                      1 => array( 
                                                                                      'from'   => array( 'forums' => 'f' ),
                                                                                      'where'  => 'f.id=t.forum_id',
                                                                                      'type'   => 'left' ))
                                                )
        );
        /*
         * Paging
         */
        require_once( ROOT_PATH . 'sources/action_public/Common.php' );
        $this->common = new Common();
        $this->common->ipsclass = & $this->ipsclass;
        $pages = $this->common->create_customize_paging($stats['count'], $per_page, $start, "", $this->ipsclass->lang['tpl_pages'],'#', TRUE, "diaries_paging");
        $vars = array();
        $vars['pages'] = $pages;
        $vars['total_diaries'] = count($diaries);
        $vars['diaries'] = $diaries;
        $output = $this->ipsclass->compiled_templates['skin_regportal']->skin_diaries($vars);
        return $output;
        //echo $output;
        //exit();
    }
    function filter_title($title){
        if($title == ""){
            return $title;
        }
        $titles =array();
        $titles = preg_split("/[\s ]+/", $title);
        $title = implode("-", $titles);
        unset ($titles);
        return $title;
    }
    function replace_unicode($str){ 
        if(!$str) return ""; 
            $unicode = array( 
                'a'=>'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ', 
                'd'=>'đ', 
                'e'=>'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ', 
                'i'=>'í|ì|ỉ|ĩ|ị', 
                'o'=>'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ', 
                'u'=>'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự', 
                'y'=>'ý|ỳ|ỷ|ỹ|ỵ', 
            ); 
            foreach($unicode as $nonUnicode=>$uni) 
                $str = preg_replace("/($uni)/i",$nonUnicode,$str); 
            return $str; 
    } 
}
?>
